package yunwo.cn.yzstation.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import android.widget.EditText;
import android.widget.TextView;

import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.api.okapi.RegistCall;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.RegistBean;

/**
 * Created by lao on 2017/9/6.
 */
public class RegistActivity extends MyBaseActivity  implements View.OnClickListener{

    private TextView mNext;
    private Intent mIntent;
    private EditText mVerification;
    private EditText mUserphone;
    private TextView mDaoijshi;
    private TextView mDuanxinyz;
    // 总倒计时时间
    private static final long MILLIS_IN_FUTURE = 60 * 1000;
    // 每次减去1秒
    private static  long COUNT_DOWN_INTERVAL = 1000;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {

        mNext = (TextView) findViewById(R.id.next);
        mVerification = (EditText) findViewById(R.id.Verification);
        mUserphone = (EditText) findViewById(R.id.phone);
        mDaoijshi = (TextView) findViewById(R.id.daoijshi);
        mDuanxinyz = (TextView) findViewById(R.id.duanxinyz);
        mNext.setOnClickListener(this);
        mDuanxinyz.setOnClickListener(this);
    }

    @Override
    protected String settiltile() {
        return "手机号注册";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_regist;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next:
                if (!mUserphone.getText().toString().trim().equals("")
                        &&!mVerification.getText().toString().trim().equals("")) {
                    mIntent = new Intent(RegistActivity.this, RegistnextActivity.class);
                    mIntent.putExtra("userhone", mUserphone.getText().toString().trim());
                    mIntent.putExtra("Verification", mVerification.getText().toString().trim());
                    startActivity(mIntent);
                }
                break;
            case R.id.duanxinyz:
                startCountDown();
                senSMS();
                break;
        }
    }

    private void senSMS() {
        OkHttpUtils.post().url(MyConstants.sen_SMS)
                .addParams("mobile",mUserphone.getText().toString().trim())
                .build().execute(new RegistCall() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.d("sms", e.toString());
            }

            @Override
            public void onResponse(RegistBean response, int id) {
                Log.d("sms", response.getCode() + ""+response.getData()+"");
            }
        });
    }

    public void startCountDown() {
        // 设置按钮为不可点击，并修改显示背景
        mDuanxinyz.setEnabled(false);
//        setBackgroundDrawable(mDisableBackground);
        // 开始倒计时
        new CountDownTimer(MILLIS_IN_FUTURE, COUNT_DOWN_INTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 刷新文字
                mDuanxinyz.setText(RegistActivity.this.getString(R.string.reget_sms_code_countdown, millisUntilFinished / COUNT_DOWN_INTERVAL));
            }

            @Override
            public void onFinish() {
//                Toast.makeText(RegistActivity.this, "刷新成", Toast.LENGTH_SHORT).show();
                mDuanxinyz.setEnabled(true);
                // 重置文字，并恢复按钮为可点击
                mDuanxinyz.setText(R.string.reget_sms_code);
//                setBackgroundDrawable(mNormalBackground);
            }
        }.start();
    }

}
